package com.ywyself.controller.admin;

import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.ywyself.common.pool.DataPool;
import com.ywyself.controller.base.YsController;
import com.ywyself.ext.kit.YsKit;
import com.ywyself.model.Diary;
import com.ywyself.service.ArticleService;
import com.ywyself.service.CatelogService;
import com.ywyself.service.DiaryService;

/**
 * 内容管理，分类和文章的管理
 * 
 * @author YuWenYue
 * @date 2017年10月23日 下午9:41:39
 */
public class ContentController extends YsController {

    /**
     * 内容管理 - 分类管理
     */
    @ActionKey(ADMIN_ROOT + "/content")
    public void index() {
        forwardAction(ADMIN_ROOT + "/content/catelog");
    }

    /**
     * 内容管理 - 分类管理
     */
    @ActionKey(ADMIN_ROOT + "/content/catelog")
    public void catelog() {
        int pageNumber = getParaToInt(0, 1);
        int pageSize = getParaToInt(1, DataPool.getPageSize());
        Page<Record> pageCatelogRec = CatelogService.EXC.pageByCond(pageNumber, pageSize, "");
        setAttr("page", pageCatelogRec);
        setAttr("tab", "content_catelog");
        setPageTitle("分类管理");
        render(BACK_STAGE + "/content/catelog.html");
    }

    @ActionKey(ADMIN_ROOT + "/content/catelog/add/save")
    public void catelogAddSave() {
        // 分类名称
        String name = getPara("name");
        // 分类描述
        String desc = getPara("desc");
        YsKit ret = CatelogService.EXC.save(name, desc);
        if (ret.isOk()) {
            renderJson(ret.code("101"));
        } else {
            renderJson(ret);
        }
    }

    /**
     * 删除
     */
    @ActionKey(ADMIN_ROOT + "/content/catelog/delete/one")
    public void catelogDeleteOne() {
        // 分类id
        String cid = getPara("cid");
        YsKit ret = CatelogService.EXC.delete(Long.parseLong(cid));
        if (ret.isOk()) {
            renderJson(ret.code("101"));
        } else {
            renderJson(ret);
        }
    }

    /**
     * 更新 - 保存
     */
    @ActionKey(ADMIN_ROOT + "/content/catelog/edit/save")
    public void catelogEditSave() {
        // 分类id
        String cid = getPara("cid");
        // 分类名称
        String name = getPara("name");
        // 分类描述
        String desc = getPara("desc");
        YsKit ret = CatelogService.EXC.update(Long.parseLong(cid), name, desc);
        if (ret.isOk()) {
            renderJson(ret.code("101"));
        } else {
            renderJson(ret);
        }
    }

    /**
     * 内容管理 - 文章管理
     */
    @ActionKey(ADMIN_ROOT + "/content/article")
    public void article() {
        int currentPageNumber = getParaToInt(0, 1);
        int pageSize = getParaToInt(1, DataPool.getPageSize());
        Page<Record> page = ArticleService.EXC.page(currentPageNumber, pageSize);
        setAttr("page", page);
        setAttr("tab", "content_article");
        setPageTitle("文章管理");
        render(BACK_STAGE + "/content/article.html");
    }

    /**
     * 内容管理 - 文章管理 - 添加文章
     */
    @ActionKey(ADMIN_ROOT + "/content/article/add")
    public void articleAdd() {
        setPageTitle("添加文章");
        render(BACK_STAGE + "/content/article.add.html");
    }

    /**
     * 内容管理 - 言语收录
     */
    @ActionKey(ADMIN_ROOT + "/content/diary")
    public void diary() {
        int pageNumber = getParaToInt(0, 1);
        int pageSize = getParaToInt(1, DataPool.getPageSize());
        setAttr("tab", "content_diary");
        setPageTitle("言语收录");
        Page<Record> page = DiaryService.EXC.page(pageNumber, pageSize);
        setAttr("page", page);
        render(BACK_STAGE + "/content/diary.html");
    }

    /**
     * 内容管理 - 言语收录 - 增加
     */
    @ActionKey(ADMIN_ROOT + "/content/diary/add")
    public void diaryAdd() {
        setAttr("postUrl", ADMIN_ROOT + "/content/diary/add/save");
        setPageTitle("新增收录");
        setAttr("tab", "content_diary");
        render(BACK_STAGE + "/content/diary.edit.html");
    }

    /**
     * 内容管理 - 言语收录 - 增加 - 保存
     */
    @ActionKey(ADMIN_ROOT + "/content/diary/add/save")
    public void diaryAddSave() {
        // 作者信息
        String author = getPara("author");
        // 时间场景
        String remark = getPara("remark");
        // 收录内容
        String content = getPara("content");
        YsKit ret = DiaryService.EXC.save(content, author, remark);
        YsKit.ok().code("100").msg("author:" + author).render(this);
        if (ret.isOk()) {
            ret.code("102").set("url", ADMIN_ROOT + "/content/diary").render(this);
        } else {
            ret.render(this);
        }
    }

    /**
     * 内容管理 - 言语收录 - 修改 - 保存
     */
    @ActionKey(ADMIN_ROOT + "/content/diary/edit")
    public void diaryEdit() {
        String id = getPara("id");
        Diary diary = DiaryService.EXC.get(Long.parseLong(id));
        setAttr("diary", diary);
        setAttr("postUrl", ADMIN_ROOT + "/content/diary/edit/save");
        setPageTitle("修改收录");
        setAttr("tab", "content_diary");
        render(BACK_STAGE + "/content/diary.edit.html");

    }

    /**
     * 内容管理 - 言语收录 - 修改 - 保存
     */
    @ActionKey(ADMIN_ROOT + "/content/diary/edit/save")
    public void diaryEditSave() {
        // id
        String id = getPara("id");
        // 作者信息
        String author = getPara("author");
        // 时间场景
        String remark = getPara("remark");
        // 收录内容
        String content = getPara("content");
        YsKit ret = DiaryService.EXC.update(Long.parseLong(id), content, author, remark);
        if (ret.isOk()) {
            ret.code("102").set("url", ADMIN_ROOT + "/content/diary").render(this);
        } else {
            ret.render(this);
        }
    }

    /**
     * 内容管理 - 言语收录 - 删除 - 单个
     */
    @ActionKey(ADMIN_ROOT + "/content/diary/delete/one")
    public void diaryDeleteOne() {
        // id
        String id = getPara("id");
        boolean flag = DiaryService.EXC.delete(Long.parseLong(id));
        if (flag) {
            YsKit.ok().code("101").msg("删除成功").render(this);
        } else {
            YsKit.fail().code("400").msg("删除失败").render(this);
        }
    }
}